package com.yunbao.project.basedata.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunbao.project.basedata.entity.GoodsCategory;
import com.yunbao.project.basedata.mapper.GoodsCategoryMapper;
import com.yunbao.project.basedata.service.IGoodsCategoryService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 商品分类 服务实现类
 * </p>
 *
 * @author 安能
 * @since 2019-09-16
 */
@Service
public class GoodsCategoryServiceImpl extends ServiceImpl<GoodsCategoryMapper, GoodsCategory> implements IGoodsCategoryService {


    @Resource
    GoodsCategoryMapper goodsCategoryMapper;

    @Override
    public List<GoodsCategory> selectTreeList() {
        List<GoodsCategory> goodsCategories = goodsCategoryMapper.selectList(
                new QueryWrapper<GoodsCategory>().eq("parent_id", 0));
        recursionTreeList(goodsCategories);

        return goodsCategories;
    }

    private void recursionTreeList(List<GoodsCategory> goodsCategories) {

        for (GoodsCategory goodsCategory : goodsCategories) {
            List<GoodsCategory> children = goodsCategoryMapper.selectList(
                    new QueryWrapper<GoodsCategory>()
                            .eq("parent_id", goodsCategory.getId()
                            )
            );
            goodsCategory.setChildren(children);
            recursionTreeList(children);
        }
    }

    @Override
    public int deleteRecursionById(Integer id) {
        List<GoodsCategory> children = goodsCategoryMapper.selectList(
                new QueryWrapper<GoodsCategory>().eq("parent_id", id));
        ArrayList<Integer> idList = new ArrayList<>();
        idList.add(id);
        recursionById(children,idList);
        goodsCategoryMapper.deleteBatchIds(idList);
        return 0;
    }

    private void recursionById(List<GoodsCategory> goodsCategories,List<Integer> idList) {

        for (GoodsCategory goodsCategory : goodsCategories) {
            List<GoodsCategory> children = goodsCategoryMapper.selectList(
                    new QueryWrapper<GoodsCategory>()
                            .eq("parent_id", goodsCategory.getId()
                            )
            );
            idList.add(goodsCategory.getId());
            recursionById(children,idList);

        }
    }


    @Override
    public List<GoodsCategory> selectTreeList(int nodeIndex) {

        List<GoodsCategory> goodsCategories = goodsCategoryMapper.selectList(
                new QueryWrapper<GoodsCategory>()
                        .eq("parent_id", nodeIndex)
        );
        for (GoodsCategory goodsCategory : goodsCategories) {
            List<GoodsCategory> children = goodsCategoryMapper.selectList(
                    new QueryWrapper<GoodsCategory>()
                            .eq("parent_id", goodsCategory.getId()
                            )
            );
            goodsCategory.setChildren(children);
            selectTreeList(goodsCategory.getId());
        }
        return goodsCategories;
    }

    public static void main(String[] args) {
        System.out.println(RandomUtil.randomNumbers(9));
    }

}
